﻿using Gengens.Disk.Entity.bean;
using System.Reflection;

namespace Gengens.Disk.Entity
{
    /// <summary>
    /// 数据库上下文配置
    /// </summary>
    public class AppDbCtxConfig
    {
        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        public string ConnectString { get; set; }

        /// <summary>
        /// 实体信息列表
        /// </summary>
        public List<ModelInfo> modelInfos { get; set; }

        public AppDbCtxConfig(string connectString,List<Assembly> assems)
        {
            this.ConnectString = connectString;
            List<Type> tys = assems.SelectMany(a => a.GetTypes()).ToList();
            string baseFullName = typeof(BaseModel<>).GetGenericTypeDefinition().FullName;
            List<Type> modelTys = tys.FindAll(t =>
            {
                Type tType = t.BaseType;
                if (tType == default || !tType.IsGenericType) return false;
                return tType.GetGenericTypeDefinition().FullName == baseFullName;
            });
            modelInfos = modelTys.ConvertAll(m => new ModelInfo(m));
        }

    }
}
